<template>
  <div>
    <el-tree ref="tree" node-key="id" check-strictly show-checkbox default-expand-all :props="{label:'name'}" :data="treelist" />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="hchlesr">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoleDetail, getPermissionList, assignPerm } from '@/api/settings'
import { trenDatatoTree } from '@/utils/index'
export default {
  name: 'Zizizi',
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      treelist: []
    }
  },

  mounted() {

  },
  created() {
    this.loadlist()
    this.putlist()
  },

  methods: {
    async putlist() {
      try {
        const res = await getRoleDetail(this.id)
        console.log('getRoleDetail', res)

        this.$refs.tree.setCheckedKeys(res.data.permIds)

        // 保存数据
      } catch (err) {
        console.log('getRoleDetail', err)
      }
    },
    async loadlist() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        this.treelist = trenDatatoTree(res.data)
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    },
    async postlist() {
      try {
        const pererer = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({ id: this.id, permIds: pererer })
        console.log('assignPerm', res)
        // 保存数据
      } catch (err) {
        console.log('assignPerm', err)
      }
    },
    hSubmit() {
      this.postlist()
      this.$emit('success')
    },
    hchlesr() {
      this.$emit('success')
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
